%{
	#include <sstream>
	#include <iostream>
	
	#include "parser_css.h"
	
	using namespace std;
	using namespace parsers;
%}
	
%option noyywrap
%option case-insensitive
%option c++

NL			(\n|\r\n|\r)

/* In these cases the - can't be mistaken for a range operator */
INT			[-+]?[0-9]+
ID			[a-z-][_a-z0-9-]*
HEX			([0-9a-f]{3}|[0-9a-f]{6})

/* We might want to replace this, because it might depend on the machine's local */
WS			[[:space:]]

/* note [(-~] matches ascii range 0x28 - 0x7E */
STRING_C	[!#$%&]|[(-~]|{NL}|{WS}

/* match strings between " or between ' */
STRING		(\"({STRING_C}|\')*\")|(\'({STRING_C}|\")*\')

/* no support for scientific notation yet */
FLOAT		{INT}+"."[0-9]*
NUMBER		{INT}|{FLOAT}

/* yes this is yucky, it parses c-style comments */
COMMENT		\/\*[^*]*\*+([^/][^*]*\*+)*\/

%%

({WS}+)|({NL}+)					{return tok_whitespace;}
{COMMENT}						{return tok_comments;}
"{"								{return tok_class_start;}
"}"								{return tok_class_end;}
";"								{return tok_end_of_statement;}
":"								{return tok_assignment;}
"["								{return tok_selector_start;}
"="								{return tok_selector_eq;}
"~="							{return tok_selector_includes;}
"]"								{return tok_selector_end;}
{INT}"px"						{return tok_pixel;}
{NUMBER}"%"						{return tok_percentage;}
{FLOAT}							{return tok_float;}
{INT}							{return tok_integer;}
{STRING}						{return tok_string;}
"#"{HEX}						{return tok_hex;}
"#"{ID}							{return tok_id;}
"."{ID}							{return tok_class;}
{ID}							{return tok_name;}
"!"{WS}*"important"				{return tok_important;}
"url("{WS}*{STRING}{WS}*")"		{return tok_url;} /* also need support for url() without quotes */

%%

void parser_css::parse(std::istream &a_Input){
	yyFlexLexer l_Lexer;
	l_Lexer.switch_streams(&a_Input, 0);
	
	while(int l_Result = l_Lexer.yylex()){
		handle_token((tokens_css)l_Result, l_Lexer.YYText());
	}
}
